<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Adv;
use app\common\model\Banner;
use app\common\model\Cate;
use app\common\model\Purchase as PurchaseModel;

class Purchase
{
    //程宇
    //获取采购大厅
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取banner
            'cate' => $this->cate(),//获取分类
            'adv' => $this->adv(),//获取广告位
        ]);
    }

    /**
     * 获取banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 4])
            ->field('id,title,image')
            ->limit(0, 1)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }

    /**
     * 获取分类
     */
    public function cate()
    {
        $model = New Cate();
        $cate = $model->where(['status' => 1, 'type' => 5])
            ->field('id,name')
            ->limit(0, 6)
            ->select();
        return $cate;
    }

    /**
     * 获取列表分类
     */
    public function cates()
    {
        $pid = input('id/d');
        $model = New Cate();
        if (empty($pid)) {
            $id = $model->where(['status' => 1, 'type' => 5])
                ->field('id')
                ->limit(0, 6)
                ->select()->toArray();
            $id = array_column($id, 'id');
            $cates1 = $model->where('status', 1)
                ->where('pid', 'in', $id)
                ->order('id', 'desc')
                ->limit(0, 8)
                ->field('id,name')
                ->select();
            $cates2 = $model->where('status', 1)
                ->where('pid', 'in', $id)
                ->field('id,name')
                ->order('id', 'desc')
                ->limit(12, 8)
                ->select();
            $cates3 = $model->where('status', 1)
                ->where('pid', 'in', $id)
                ->field('id,name')
                ->order('id', 'desc')
                ->limit(20, 14)
                ->select();
            $cates4 = $model->where('status', 1)
                ->where('pid', 'in', $id)
                ->field('id,name')
                ->order('id', 'desc')
                ->limit(34, 16)
                ->select();
            return sucessMsg('200', '获取成功',
                ['cates1' => $cates1,
                    'cates2' => $cates2,
                    'cates3' => $cates3,
                    'cates4' => $cates4
                ]);
        } else {
            $cates1 = $model->where(['status' => 1, 'pid' => $pid])
                ->order('id', 'desc')
                ->limit(0, 8)
                ->field('id,name')
                ->select();
            $cates2 = $model->where(['status' => 1, 'pid' => $pid])
                ->order('id', 'desc')
                ->limit(12, 8)
                ->field('id,name')
                ->select();
            $cates3 = $model->where(['status' => 1, 'pid' => $pid])
                ->order('id', 'desc')
                ->limit(20, 14)
                ->field('id,name')
                ->select();
            return sucessMsg('200', '获取成功',
                ['cates1' => $cates1,
                    'cates2' => $cates2,
                    'cates3' => $cates3,
                ]);
        }


    }

    /**
     * 获取广告位
     */
    public function adv()
    {
        $model = New Adv();
        $adv = $model->where(['status' => 1, 'type' => 2])
            ->field('id,image,title')
            ->limit(0, 1)
            ->find();
        $adv['image'] = env('app.yuming') . $adv['image'];
        return $adv;
    }

    /**
     * 获取列表
     */
    public function list()
    {
        $model = New PurchaseModel();
        $id = input('id/d');
        $page = input('page/d', 1);
        $pagesize = 10;
        if (empty($id)) {
            $cate = New Cate();
            $id = $cate->where(['status' => 1, 'type' => 5])
                ->field('id')
                ->limit(0, 6)
                ->select()->toArray();
            $id = array_column($id, 'id');
            $cates = $cate->where('status', 1)
                ->where('pid', 'in', $id)
                ->order('id', 'desc')
                ->field('id,name')
                ->select()->toArray();
            $cates = array_column($cates, 'id');
            $list = $model->where('status', 1)
                ->where('cate_id', 'in', $cates)
                ->field('id,name,number,address,publisher,certification')
                ->page($page, $pagesize)
                ->select();
            $count = count($model->where('status', 1)
                ->where('cate_id', 'in', $cates)
                ->select());
            $maxpage = ceil($count / $pagesize);
            return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize' => $pagesize]);
        } else {
            $cate = New Cate();
            $cates = $cate->where(['status' => 1, 'id' => $id])
                ->field('id,pid')
                ->find();
            if ($cates['pid'] == 0) {
                $cates = $cate->where(['status' => 1, 'id' => $id])
                    ->field('id,pid')
                    ->select()->toArray();
                $pid = array_column($cates, 'id');
                $aa = $cate->where('status', 1)
                    ->where('pid', 'in', $pid)
                    ->order('id', 'desc')
                    ->field('id,name')
                    ->select()->toArray();
                $cate_id = array_column($aa, 'id');
                $list = $model->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,name,number,address,publisher,certification')
                    ->page($page, $pagesize)
                    ->select();
                $count = count($model->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->select());
                $maxpage = ceil($count / $pagesize);
                return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'pagesize' => $pagesize ,'maxpage'=>$maxpage]);
            } else {
                $aa = $cate->where(['status' => 1, 'id' => $id])
                    ->order('id', 'desc')
                    ->field('id,name')
                    ->select()->toArray();
                $cate_id = array_column($aa, 'id');
                $list = $model->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,name,number,address,publisher,certification')
                    ->page($page, $pagesize)
                    ->select();
                $count = count($model->where('status', 1)->select());
                $maxpage = ceil($count / $pagesize);
                return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize'=>$pagesize]);
            }
        }
    }

    public function content()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg('400', '请传入id');
        }
        $model = New PurchaseModel();
        $content = $model->where(['status' => 1, 'id' => $id])
            ->field('id,name,number,address,region,guige,created_at')
            ->find();
        $xinxi = $model->where(['status' => 1, 'id' => $id])
            ->field('id,publisher,join,certification,image')
            ->find();
        $xinxi['image'] = env('app.yuming') . $xinxi['image'];
        $list = $model->where('status', 1)
            ->field('id,name,number,address,publisher,certification')
            ->order('id', 'desc')
            ->limit(0, 6)
            ->select();
        return sucessMsg('200', '获取成功', ['content' => $content, 'xinxi' => $xinxi, 'list' => $list]);
    }

}
